package com.soul.soulcommon.config;

import com.soul.soulcommon.detail.DoctorDetail;
import com.soul.soulcommon.detail.service.DoctorDetailService;
import com.soul.soulcommon.detail.service.GuardianDetailService;
import com.soul.soulcommon.detail.service.OlderDetailService;
import com.soul.soulcommon.filter.JwtAuthencationFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@EnableMethodSecurity
@Slf4j
@Configuration
@EnableWebSecurity
public class SercurityConfig{
    @Autowired
    private OlderDetailService olderDetailService;
    @Autowired
    private DoctorDetailService doctorDetailService;
    @Autowired
    private GuardianDetailService detailService;
    @Autowired
    private JwtAuthencationFilter jwtAuthencationFilter;
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(auth -> auth.requestMatchers(
                "/email/get/**",
                "/email/**",
                "/swagger-ui/swagger-config",
                        "/swagger-ui/**",
                "/v3/api-docs",
                "/v3/api-docs/swagger-config",
                "/user/older/login").permitAll()
                .anyRequest().permitAll());
//                .formLogin(form -> form
//                        .loginProcessingUrl("/user/older/login") // 登录处理的URL
//                        .failureHandler(customAuthenticationFailureHandler) // 设置自定义的失败处理器
//                );
        http.csrf(AbstractHttpConfigurer::disable);
        http.addFilterBefore(jwtAuthencationFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }
    @Primary
    @Bean("olderAuthenticationManager")
    public AuthenticationManager olderAuthenticationManager(){
        DaoAuthenticationProvider olderDaoAuthenticationProvider = new DaoAuthenticationProvider();
        olderDaoAuthenticationProvider.setUserDetailsService(olderDetailService);
        olderDaoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return new ProviderManager(olderDaoAuthenticationProvider);
    }
    @Bean("doctorAuthenticationManager")
    public AuthenticationManager doctorAuthenticationManager(){
        DaoAuthenticationProvider doctorDaoAuthenticationProvider = new DaoAuthenticationProvider();
        doctorDaoAuthenticationProvider.setUserDetailsService(doctorDetailService);
        doctorDaoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return new ProviderManager(doctorDaoAuthenticationProvider);
    }
    @Bean("guardianAuthenticationManager")
    public AuthenticationManager guardianAuthenticationManager(){
        DaoAuthenticationProvider guardianDaoAuthenticationProvider = new DaoAuthenticationProvider();
        guardianDaoAuthenticationProvider.setUserDetailsService(detailService);
        guardianDaoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return new ProviderManager(guardianDaoAuthenticationProvider);
    }
    @Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }
}
